import 'package:flutter/material.dart';
import 'package:flutter_house_manager/pages/Home/index.dart';
import 'package:flutter_house_manager/pages/Mine/index.dart';

class TabsPage extends StatefulWidget {
  const TabsPage({super.key});

  @override
  State<TabsPage> createState() => _TabsPageState();
}

class _TabsPageState extends State<TabsPage> {
  final List _tabList = [
    {
      "label": '首页',
      "icon": "assets/tabs/home_default.png",
      "active_icon": "assets/tabs/home_active.png"
    },
    {
      "label": '我的',
      "icon": "assets/tabs/my_default.png",
      "active_icon": "assets/tabs/my_active.png"
    }
  ];

  //返回底部tabs中的item
  List<BottomNavigationBarItem> _getBottomTabs() {
    //遍历tabList中的每个item
    return _tabList.map((item) {
      return BottomNavigationBarItem(
        icon: Image.asset(
          item["icon"],
          width: 30,
          height: 30,
        ),
        activeIcon: Image.asset(
          item["active_icon"],
          width: 30,
          height: 30,
        ),
        label: item["label"],
      );
    }).toList();
  }

  // 当前选中的索引
  int _currentIndex = 0;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: SafeArea(
          child: IndexedStack(
        index: _currentIndex,
        children: const [
          HomePage(),
          MineView(),
        ],
      )),
      //底部导航栏
      bottomNavigationBar: BottomNavigationBar(
        currentIndex: _currentIndex, //当前选中的索引
        selectedItemColor: const Color.fromARGB(255, 85, 145, 175), //选中项的颜色
        onTap: (index) {
          //点击事件
          setState(() {
            //引起UI更新
            _currentIndex = index; //高亮
          });
        },
        items: _getBottomTabs(), //获取底部tabs中的item
      ),
    );
  }
}
